package com.dimong.loan.sdk.net;

import android.util.Log;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by wuligang on 2018/4/26.
 */

public abstract class NetHttpCallback extends Callback<BaseResponse> {
    /**
     * UI Thread
     *
     * @param request
     */
    public void onBefore(Request request, int id) {
        onPreRequest();
    }

    /**
     * 非UI线程
     *
     * @param response
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public BaseResponse parseNetworkResponse(Response response, int id) throws Exception {
        String mResult = response.body().string();
        BaseResponse t = new Gson().fromJson(mResult, BaseResponse.class);
        return t;
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        onError("-10086", "网络请求超时");
    }

    @Override
    public void onResponse(BaseResponse response, int id) {
        if ("200".equals(response.getRet())) {
            onResult(response.getData().toString());
        } else {
            onError(response.getRet(), response.getMsg());
        }
    }

    public void onPreRequest() {

    }

    public void onResult(String result) {

    }

    public void onError(String errorCode, String error) {

    }
}
